package com.example.service.Impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.mapper.*;
import com.example.pojo.entity.Inbound;
import com.example.pojo.entity.Inventory;
import com.example.pojo.entity.Material;
import com.example.pojo.entity.Warehouse;
import com.example.service.IInboundService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.service.IInventoryService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 物资入库 服务实现类
 * </p>
 *
 * @author wy
 * @since 2024-07-17
 */
@Service
public class InboundServiceImpl extends ServiceImpl<InboundMapper, Inbound> implements IInboundService {

    @Autowired
    private InboundMapper inboundMapper;

    @Autowired
    private IInventoryService iInventoryService;

    @Override
    public Page<Inbound> selectPage(Inbound inbound, Integer pageNum, Integer pageSize) {
        MPJLambdaWrapper<Inbound> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper
                .selectAll(Inbound.class)
                .selectAs(Warehouse::getName,Inbound::getWarehouseName)
                .selectAs(Material::getName, Inbound::getMaterialName)
                .innerJoin(Warehouse.class,Warehouse::getId,Inbound::getWarehouseId)
                .innerJoin(Material.class,Material::getId,Inbound::getMaterialId)
                .eq(ObjectUtil.isNotNull(inbound.getWarehouseId()), Inbound::getWarehouseId, inbound.getWarehouseId())
                .eq(ObjectUtil.isNotNull(inbound.getMaterialId()), Inbound::getMaterialId, inbound.getMaterialId());

        return page(new Page<>(pageNum, pageSize), mpjLambdaWrapper);
    }

    @Override
    public void add(Inbound inbound) {
        MPJLambdaWrapper<Inventory> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper
                .eq(Inventory::getMaterialId,inbound.getMaterialId())
                .eq(Inventory::getWarehouseId,inbound.getWarehouseId())
                .eq(Inventory::getIsDeleted,0);
        Inventory one = iInventoryService.getOne(mpjLambdaWrapper);

        if(ObjectUtil.isNull(one)){
            Inventory inventory = Inventory.builder()
                    .materialId(inbound.getMaterialId())
                    .warehouseId(inbound.getWarehouseId())
                    .stockQuantity(inbound.getInboundQuantity())
                    .build();
            iInventoryService.add(inventory);
            inbound.setVersion(0);
            inboundMapper.insert(inbound);
        }else{
            if(!ObjectUtil.isNull(one)){
                one.setStockQuantity(one.getStockQuantity()+inbound.getInboundQuantity());
                iInventoryService.updateById(one);
                inbound.setVersion(0);
                inboundMapper.insert(inbound);
            }
        }
    }


}
